﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Text;
using Acms.AcmsControls;
using Acms.Bll;

public partial class Templates_VideoHolder_FlashXml : Acms.AcmsControls.TemplatePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Response.Clear();
            Response.ContentType = "text/xml";
            XmlTextWriter xml = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);
            xml.WriteStartDocument();

            xml.WriteStartElement("data");

            WriteStripImage(xml);

            //xml.WriteStartElement("VIDEOS");
            //for (int i = 1; i <= 3; i++)
            //{
            //    WriteVideo(xml, i);
            //}
            //xml.WriteEndElement();//"VIDEOS"

            xml.WriteEndElement();//"DATA"
            xml.Close();
            Response.Flush();

            Response.End();
        }
    }

    //private void WriteVideo(XmlTextWriter xml, int i)
    //{
    //    var videoControl = AcmsContext.CurrentPost.FindPostControl(string.Format("Flash Video {0}", i));
    //    var xControl = AcmsContext.CurrentPost.FindPostControl(string.Format("Flash Video {0} X", i));
    //    var yControl = AcmsContext.CurrentPost.FindPostControl(string.Format("Flash Video {0} Y", i));
    //    if (videoControl != null)
    //    {
    //        var videoProperties = videoControl.GetProperties<Acms.AcmsControls.FlashVideoContentControlProperties>();
    //        if (videoProperties != null && videoProperties.HasResource())
    //        {
    //            int x = 50;
    //            if (xControl != null)
    //            {
    //                var xProperties = xControl.GetProperties<Acms.AcmsControls.TextContentControlProperties>();
    //                if (xProperties != null)
    //                {
    //                    try
    //                    {
    //                        x = int.Parse(xProperties.Text.Trim());
    //                    }
    //                    catch (FormatException)
    //                    {
    //                    }
    //                }
    //            }
    //            int y = 50;
    //            if (yControl != null)
    //            {
    //                var yProperties = yControl.GetProperties<Acms.AcmsControls.TextContentControlProperties>();
    //                if (yProperties != null)
    //                {
    //                    try
    //                    {
    //                        y = int.Parse(yProperties.Text.Trim());
    //                    }
    //                    catch (FormatException)
    //                    {
    //                    }
    //                }
    //            }

    //            xml.WriteStartElement("VIDEO");

    //            //xml.WriteAttributeString("itemURL", AcmsContext.FullyQualifiedUrl(RenderFile.GetUrl(videoProperties.ResourceId, this), this));
    //            xml.WriteAttributeString("itemURL", AcmsContext.FullyQualifiedUrl(RenderFile.GetUrl(videoProperties.ResourceId, this), this));
    //            xml.WriteAttributeString("posX", x.ToString());
    //            xml.WriteAttributeString("posY", y.ToString());

    //            xml.WriteEndElement();//VIDEO
    //        }
    //    }
    //}

    private void WriteStripImage(XmlTextWriter xml)
    {
        //stripImageControl = AcmsContext.CurrentPost.Children[0]
            foreach(Post p in AcmsContext.CurrentPost.Children){
                
                var stripImageControl = p.FindPostControl("CarouselImage");
                if (stripImageControl != null)
                {
                    var stripImageProperties = stripImageControl.GetProperties<Acms.AcmsControls.ImageContentControlProperties>();
                    if (stripImageProperties != null && stripImageProperties.HasResource())
                    {
                        xml.WriteStartElement("image");
                        //xml.WriteAttributeString("imageUrl",AcmsContext.FullyQualifiedUrl(RenderFile.GetUrl(stripImageProperties.ResourceId, this), this));
                        xml.WriteCData(AcmsContext.FullyQualifiedUrl(RenderFile.GetUrl(stripImageProperties.ResourceId, this), this));
                        xml.WriteEndElement();//IMAGE
                    }
                }

                var pageUrl = p.Url;
                if (pageUrl!= null)
                {
                    //var stripImageProperties = stripImageControl.GetProperties<Acms.AcmsControls.ImageContentControlProperties>();
                    //if (stripImageProperties != null && stripImageProperties.HasResource())
                    //{
                        xml.WriteStartElement("pageUrl");
                        //xml.WriteAttributeString("imageUrl",AcmsContext.FullyQualifiedUrl(RenderFile.GetUrl(stripImageProperties.ResourceId, this), this));
                        xml.WriteCData(AcmsContext.FullyQualifiedUrl(pageUrl,this));
                        xml.WriteEndElement();//PAGEURL
                    //}
                }

                var displayNamepost = p.DisplayName;
                if (displayNamepost != null)
                {
                    xml.WriteStartElement("displayName");
                    xml.WriteCData(displayNamepost);
                    xml.WriteEndElement();//PAGEURL
                    
                }
            }
        }
    }

